package com.ruoyi.project.configureSystem.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.configureSystem.domain.VDatatype;
import com.ruoyi.project.configureSystem.domain.VStation;
import com.ruoyi.project.configureSystem.mapper.VDatatypeMapper;
import com.ruoyi.project.configureSystem.mapper.VStationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.configureSystem.mapper.VDatainfoMapper;
import com.ruoyi.project.configureSystem.domain.VDatainfo;
import com.ruoyi.project.configureSystem.service.IVDatainfoService;

/**
 * 指标信息Service业务层处理
 * 
 * @author wangshuo
 * @date 2020-05-17
 */
@Service
public class VDatainfoServiceImpl implements IVDatainfoService 
{
    @Autowired
    private VDatainfoMapper vDatainfoMapper;
    @Autowired
    private VStationMapper vStationMapper;
    @Autowired
    private VDatatypeMapper vDatatypeMapper;


    /**
     * 查询指标信息
     * 
     * @param id 指标信息ID
     * @return 指标信息
     */
    @Override
    public VDatainfo selectVDatainfoById(Long id)
    {
        VDatainfo vDatainfo = vDatainfoMapper.selectVDatainfoById(id);
        if(StringUtils.isNotNull(vDatainfo)){
            VDatatype vDatatype = vDatatypeMapper.selectVDatatypeById(vDatainfo.getDataitemid());
            vDatainfo.setProdataName(vDatatype.getTypeName());
        }
        return vDatainfo;
    }

    /**
     * 查询指标信息列表
     * 
     * @param vDatainfo 指标信息
     * @return 指标信息
     */
    @Override
    public List<VDatainfo> selectVDatainfoList(VDatainfo vDatainfo)
    {
        if(vDatainfo.getIsStation()!=null){
            List<Long> stationIds = new ArrayList<>();
            if(vDatainfo.getIsStation()==0){
                //区域，则查询下面所有的站点
                stationIds =  vStationMapper.selectVStationIdsByAreaId(vDatainfo.getAreaId());
                if(stationIds.size()==0){
                    return new ArrayList<>();
                }
                vDatainfo.setStationIds(stationIds);
            }
        }
        List<VDatainfo> vDatainfos = vDatainfoMapper.selectVDatainfoList(vDatainfo);
        vDatainfos.forEach(x->{
            if(!StringUtils.isNull(x.getDataitemid())){
                VDatatype vDatatype = vDatatypeMapper.selectVDatatypeById(x.getDataitemid());
                x.setProdataName(Optional.ofNullable(vDatatype.getTypeName()).orElse(""));
            }
        });
        return vDatainfos;
    }

    /**
     * 新增指标信息
     * 
     * @param vDatainfo 指标信息
     * @return 结果
     */
    @Override
    public int insertVDatainfo(VDatainfo vDatainfo)
    {
        if(!StringUtils.isNotBlank(vDatainfo.getStationcode())){
           throw new CustomException("厂站编号不能为空");
        }
        VStation vStation = vStationMapper.selectVStationByStationCode(vDatainfo.getStationcode());
        if(vStation ==null){
            throw new CustomException("不存在该厂站");
        }
//        vDatainfo.set
        //todo
        vDatainfo.setVstationId(vStation.getId());
        return vDatainfoMapper.insertVDatainfo(vDatainfo);
    }

    /**
     * 修改指标信息
     * 
     * @param vDatainfo 指标信息
     * @return 结果
     */
    @Override
    public int updateVDatainfo(VDatainfo vDatainfo)
    {
        return vDatainfoMapper.updateVDatainfo(vDatainfo);
    }

    /**
     * 批量删除指标信息
     * 
     * @param ids 需要删除的指标信息ID
     * @return 结果
     */
    @Override
    public int deleteVDatainfoByIds(Long[] ids)
    {
        return vDatainfoMapper.deleteVDatainfoByIds(ids);
    }

    /**
     * 删除指标信息信息
     * 
     * @param id 指标信息ID
     * @return 结果
     */
    @Override
    public int deleteVDatainfoById(Long id)
    {
        return vDatainfoMapper.deleteVDatainfoById(id);
    }

    @Override
    public List<VDatainfo> getDatainfoTreeByStationCode(String stationCode) {
        List<VDatainfo> vDatainfos= vDatainfoMapper.getDatainfoTreeByStationCode(stationCode);
        vDatainfos.forEach(x->{
            //获取指标信息
            String ids = x.getIds();
            List<VDatainfo> vDatainfoChild = vDatainfoMapper.selectVDatainfoByIds(ids);
            x.setChildren(vDatainfoChild);
        });
        return vDatainfos;
    }
}
